package com.example.lenovo.weiyi;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.com.ruanmeng.demon.CommonM;
import com.com.ruanmeng.demon.LunTanListM;
import com.com.ruanmeng.demon.ZiXunListM;
import com.com.ruanmeng.utils.CommonUtil;
import com.com.ruanmeng.utils.ImgDataUtil;
import com.com.ruanmeng.utils.Params;
import com.com.ruanmeng.utils.PreferencesUtils;
import com.com.ruanmeng.view.CircularImage;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.rest.Request;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import adapter.BaseLoadMoreHeaderAdapter;
import butterknife.BindView;
import butterknife.ButterKnife;
import nohttp.CallServer;
import nohttp.CustomHttpListener;
import share.Const;
import share.HttpIP;

import static com.com.ruanmeng.utils.Params.LTcollection_index;

/**
 * 咨询
 */
public class ZiXunActivity extends BaseActivity {

    @BindView(R.id.rb_all)
    RadioButton rbAll;
    @BindView(R.id.rb_daihuida)
    RadioButton rbDaihuida;
    @BindView(R.id.rb_answered)
    RadioButton rbAnswered;
    @BindView(R.id.rb_sc)
    RadioGroup rbSc;
    @BindView(R.id.rv_zx)
    RecyclerView rvZx;
    @BindView(R.id.rl_zx_refresh)
    SwipeRefreshLayout mRefresh;
    ArrayList<ZiXunListM.RowsBean> Temp_list = new ArrayList<>();
    ArrayList<LunTanListM.RowsBean> Temp_LTlist = new ArrayList<>();
    @BindView(R.id.li_zixun)
    LinearLayout liZixun;
    @BindView(R.id.li_luntan)
    LinearLayout liLuntan;
    @BindView(R.id.rv_lt)
    RecyclerView rvLt;
    @BindView(R.id.bt_faqiluntan)
    Button btFaqiluntan;
    private ZiXunListAdapter mAdapter;
    String statues;
    private int page = 1;
    private TextView tv_titlezixun, tv_titlelt;
    private LinearLayout li_lt_null;
    private TextView li_zixun_null;
    private LunTanListAdapter mltAdapter;
    private int type;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zu_xun);
        ButterKnife.bind(this);
        LayTitleBack();
        init();
//        getData();
    }

    @Override
    protected void onResume() {
        super.onResume();
        try {
            if (Params.fqzu == 1) {
                Params.fqzu = 0;
                page = 1;
                getData();
            }
            if (Params.fqlt == 1) {
                Params.fqlt = 0;
                page = 1;
                getLTListData();
            }
            if (!TextUtils.isEmpty(Params.LTHF_num) && !TextUtils.isEmpty(Params.LTIndex)) {
                Temp_LTlist.get(Integer.parseInt(Params.LTIndex)).setReplyCount(Integer.parseInt(Params.LTHF_num));
                Params.LTHF_num = "";
                Params.LTIndex = "";
                mltAdapter.notifyDataSetChanged();
            }
            if (!TextUtils.isEmpty(Params.LTcollection_index) && !TextUtils.isEmpty(Params.Change_Collection)) {
                if (("1").equals(Params.Change_Collection)) {
                    Temp_LTlist.get(Integer.parseInt(Params.LTcollection_index)).setIfCollect(1);
                } else {
                    Temp_LTlist.get(Integer.parseInt(Params.LTcollection_index)).setIfCollect(0);
                }
                Params.Change_Collection = "";
                Params.LTcollection_index = "";
                mltAdapter.notifyDataSetChanged();
            }
        } catch (Exception o) {
        }
    }


    public void init() {
        //咨询  论坛头部
        tv_titlezixun = (TextView) findViewById(R.id.tv_titlezixun);
        tv_titlelt = (TextView) findViewById(R.id.tv_titlelt);
        li_zixun_null = (TextView) findViewById(R.id.li_zixun_null);
        li_lt_null = (LinearLayout) findViewById(R.id.li_lt_null);
        tv_titlezixun.performClick();
        mRefresh.setColorSchemeResources(R.color.MainColor);
        mRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                if (type == 0) {
                    getData();
                } else {
                    getLTListData();
                }

            }
        });
        /**
         * 咨询
         */
        //设置布局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        rvZx.setLayoutManager(linearLayoutManager);
        //设置适配器
        mAdapter = new ZiXunListAdapter(ZiXunActivity.this, rvZx, Temp_list, R.layout.item_zixun);
        rvZx.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new BaseLoadMoreHeaderAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
            }
        });
        mAdapter.setOnLoadMoreListener(new BaseLoadMoreHeaderAdapter.OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                page++;
                if (type == 0) {
                    getData();
                } else {
                    getLTListData();
                }
            }
        });
        /**
         * 论坛
         */
        //设置布局管理器
        LinearLayoutManager linearLayoutManager1 = new LinearLayoutManager(this);
        rvLt.setLayoutManager(linearLayoutManager1);
        //设置适配器
        mltAdapter = new LunTanListAdapter(ZiXunActivity.this, rvLt, Temp_LTlist, R.layout.item_luntan);
        rvLt.setAdapter(mltAdapter);
        mltAdapter.setOnItemClickListener(new BaseLoadMoreHeaderAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
            }
        });
        mltAdapter.setOnLoadMoreListener(new BaseLoadMoreHeaderAdapter.OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                page++;
                getLTListData();
            }
        });
    }

    /**
     * 咨询列表
     */
    private void getData() {
        Request<String> mRequest = NoHttp.createStringRequest(HttpIP.ZiXunList, Const.POST);
        mRequest.addHeader("token", PreferencesUtils.getString(ZiXunActivity.this, "uid"));
        mRequest.add("status", statues);//	回复状态0：未回复，1已回复   全部“”
        mRequest.add("page", page);
        CallServer.getRequestInstance().add(ZiXunActivity.this, 0, mRequest,
                new CustomHttpListener(ZiXunActivity.this, true, ZiXunListM.class) {

                    @Override
                    public void doWork(Object data, boolean isOne) {
                        ZiXunListM model = ((ZiXunListM) data);
                        if (page == 1) {
                            Temp_list.clear();
                        }
                        Temp_list.addAll(model.getRows());
                        if (Temp_list.size() == 0) {
                            li_zixun_null.setVisibility(View.VISIBLE);
                            rvZx.setVisibility(View.GONE);
                        } else {
                            li_zixun_null.setVisibility(View.GONE);
                            rvZx.setVisibility(View.VISIBLE);
                        }
                        mAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onFinally(JSONObject obj) throws JSONException {
                        super.onFinally(obj);
                        mRefresh.setRefreshing(false);
                    }
                }, true, true);
    }

    /**
     * 论坛列表
     */
    private void getLTListData() {
        Request<String> mRequest = NoHttp.createStringRequest(HttpIP.LunTanList, Const.POST);
        mRequest.addHeader("token", PreferencesUtils.getString(ZiXunActivity.this, "uid"));
        mRequest.add("page", page);
        CallServer.getRequestInstance().add(ZiXunActivity.this, 0, mRequest,
                new CustomHttpListener(ZiXunActivity.this, true, LunTanListM.class) {
                    @Override
                    public void doWork(Object data, boolean isOne) {
                        LunTanListM model = ((LunTanListM) data);
                        if (page == 1) {
                            Temp_LTlist.clear();
                        }
                        Temp_LTlist.addAll(model.getRows());
                        if (Temp_LTlist.size() == 0) {
                            li_lt_null.setVisibility(View.VISIBLE);
                            rvLt.setVisibility(View.GONE);
                        } else {
                            li_lt_null.setVisibility(View.GONE);
                            rvLt.setVisibility(View.VISIBLE);
                        }
                        mltAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onFinally(JSONObject obj) throws JSONException {
                        super.onFinally(obj);
                        mRefresh.setRefreshing(false);
                    }
                }, true, true);
    }

    /**
     * 论坛收藏
     */
    private void getCollectionData(final int index, String c_id) {
        Request<String> mRequest = NoHttp.createStringRequest(HttpIP.Collection, Const.POST);
        mRequest.addHeader("token", PreferencesUtils.getString(ZiXunActivity.this, "uid"));
        mRequest.add("questionId", c_id);
        CallServer.getRequestInstance().add(ZiXunActivity.this, 0, mRequest,
                new CustomHttpListener(ZiXunActivity.this, true, CommonM.class) {
                    @Override
                    public void doWork(Object data, boolean isOne) {
                        CommonM model = ((CommonM) data);
                        Temp_LTlist.get(index).setIfCollect(1);
                        mltAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onFinally(JSONObject obj) throws JSONException {
                        super.onFinally(obj);
                        CommonUtil.showToask(ZiXunActivity.this, obj.getString("msg"));
                    }
                }, true, false);
    }

    /**
     * 论坛取消收藏
     */
    private void getCancellCollectionData(final int c_index, String c_id) {
        Request<String> mRequest = NoHttp.createStringRequest(HttpIP.CancelCollection, Const.POST);
        mRequest.addHeader("token", PreferencesUtils.getString(ZiXunActivity.this, "uid"));
        mRequest.add("questionId", c_id);//取消论坛时   传questionID  取消知识是  传CourseID
        CallServer.getRequestInstance().add(ZiXunActivity.this, 0, mRequest,
                new CustomHttpListener(ZiXunActivity.this, true, CommonM.class) {
                    @Override
                    public void doWork(Object data, boolean isOne) {
                        CommonM model = ((CommonM) data);
                        Temp_LTlist.get(c_index).setIfCollect(0);
                        mltAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onFinally(JSONObject obj) throws JSONException {
                        super.onFinally(obj);
                        CommonUtil.showToask(ZiXunActivity.this, obj.getString("msg"));
                    }
                }, true, false);
    }

    /**
     * 咨询列表
     */
    public class ZiXunListAdapter extends BaseLoadMoreHeaderAdapter<ZiXunListM.RowsBean> {


        public ZiXunListAdapter(Context mContext, RecyclerView recyclerView, List<ZiXunListM.RowsBean> mDatas, int mLayoutId) {
            super(mContext, recyclerView, mDatas, mLayoutId);
        }

        @Override
        public void convert(Context mContext, final RecyclerView.ViewHolder holder, ZiXunListM.RowsBean s) {
            CircularImage pic = (CircularImage) holder.itemView.findViewById(R.id.imv_zx_userpic);
            TextView tv_zx_name = (TextView) holder.itemView.findViewById(R.id.tv_zx_name);
            TextView name = (TextView) holder.itemView.findViewById(R.id.tv_zixun_title);
            LinearLayout li_huifu = (LinearLayout) holder.itemView.findViewById(R.id.li_huifu);
            TextView time = (TextView) holder.itemView.findViewById(R.id.tv_zixun_data);
            TextView tv_content = (TextView) holder.itemView.findViewById(R.id.tv_content);
            TextView tv_huifu = (TextView) holder.itemView.findViewById(R.id.tv_huifu);//查看回复
            ImageView imv_huifu = (ImageView) holder.itemView.findViewById(R.id.imv_huifu);//查看回复图片
            LinearLayout li_content = (LinearLayout) holder.itemView.findViewById(R.id.li_content);//回复内容
            LinearLayout li_man = (LinearLayout) holder.itemView.findViewById(R.id.li_huifu_man);
            ImgDataUtil.loadImage(ZiXunActivity.this, HttpIP.Base_IpIMage + s.getUserhead(), pic);
            tv_zx_name.setText(s.getUserName());
            name.setText(s.getTitle());
            time.setText(s.getCreateDate());
            tv_content.setText(s.getReplyContent());
            if (s.getCheck() == 1) {
                tv_huifu.setText("收起回复");
                imv_huifu.setBackgroundResource(R.drawable.zx_icon02_b);
                li_content.setVisibility(View.VISIBLE);
            } else {
                tv_huifu.setText("查看回复");
                imv_huifu.setBackgroundResource(R.drawable.zx_icon01_b);
                li_content.setVisibility(View.GONE);
            }
            if (!TextUtils.isEmpty(s.getReplyContent())) {//有回复内容
                li_huifu.setVisibility(View.VISIBLE);
            } else {
                li_huifu.setVisibility(View.GONE);
            }
            if (("0").equals(statues)) {//待回答没有查看回复
                li_huifu.setVisibility(View.GONE);
            }
            if (holder.getLayoutPosition() == Temp_list.size() - 1) {
                li_man.setVisibility(View.GONE);
            } else {
                li_man.setVisibility(View.VISIBLE);
            }
            li_huifu.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (Temp_list.get(holder.getLayoutPosition()).getCheck() == 0) {
                        Temp_list.get(holder.getLayoutPosition()).setCheck(1);
                    } else {
                        Temp_list.get(holder.getLayoutPosition()).setCheck(0);
                    }
                    mAdapter.notifyDataSetChanged();
                }
            });
        }
    }

    /**
     * 论坛列表
     */
    public class LunTanListAdapter extends BaseLoadMoreHeaderAdapter<LunTanListM.RowsBean> {


        public LunTanListAdapter(Context mContext, RecyclerView recyclerView, List<LunTanListM.RowsBean> mDatas, int mLayoutId) {
            super(mContext, recyclerView, mDatas, mLayoutId);
        }

        @Override
        public void convert(Context mContext, final RecyclerView.ViewHolder holder, final LunTanListM.RowsBean s) {
            CircularImage pic = (CircularImage) holder.itemView.findViewById(R.id.imv_lt_userpic);
            TextView name = (TextView) holder.itemView.findViewById(R.id.tv_lt_name);
            TextView danwei = (TextView) holder.itemView.findViewById(R.id.tv_lt_dw);
            TextView tv_lt_hfnum = (TextView) holder.itemView.findViewById(R.id.tv_lt_hfnum);
            TextView tv_content = (TextView) holder.itemView.findViewById(R.id.tv_lt_content);
            TextView tv_lt_titkle = (TextView) holder.itemView.findViewById(R.id.tv_lt_titkle);
            TextView time = (TextView) holder.itemView.findViewById(R.id.tv_lt_time);
            //收藏
            LinearLayout li_lt_collection = (LinearLayout) holder.itemView.findViewById(R.id.li_lt_collection);
            ImageView imv_lt_collection = (ImageView) holder.itemView.findViewById(R.id.imv_lt_collection);
            TextView tv_lt_collection = (TextView) holder.itemView.findViewById(R.id.tv_lt_collection);
            LinearLayout li_lt_hf = (LinearLayout) holder.itemView.findViewById(R.id.li_lt_hf);
            LinearLayout li_man = (LinearLayout) holder.itemView.findViewById(R.id.li_lt_man);
            ImgDataUtil.loadYuanImage(ZiXunActivity.this, HttpIP.Base_IpIMage + s.getUserhead(), pic);
            name.setText(s.getUserName());
//            danwei.setText(s.getCompName());
            tv_lt_titkle.setText(s.getTitle());
            time.setText(s.getCreateDate());
            tv_content.setText(s.getContent());
            //收藏有关
            li_lt_collection.setVisibility(View.VISIBLE);
            if (s.getIfCollect() == 1) {
                imv_lt_collection.setBackgroundResource(R.drawable.project_collection_pre_b);
                tv_lt_collection.setText("已收藏");
                tv_lt_collection.setTextColor(getResources().getColor(R.color.MainColor));
            } else {
                imv_lt_collection.setBackgroundResource(R.drawable.project_collection);
                tv_lt_collection.setText("收藏");
                tv_lt_collection.setTextColor(getResources().getColor(R.color.ThirdGrayColor));
            }
            if (s.getReplyCount() != 0) {
                tv_lt_hfnum.setText(s.getReplyCount() + "个回复");
            } else {
                tv_lt_hfnum.setText("0个回复");
            }
            if (holder.getLayoutPosition() == Temp_LTlist.size() - 1) {
                li_man.setVisibility(View.GONE);
            } else {
                li_man.setVisibility(View.VISIBLE);
            }
            li_lt_hf.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Params.LTIndex = holder.getLayoutPosition() + "";
                    LTcollection_index = holder.getLayoutPosition() + "";
                    Intent in = new Intent(ZiXunActivity.this, LunTanXQActivity.class);
                    in.putExtra("id", s.getQuestionId());
                    in.putExtra("name", s.getUserName());
//                    in.putExtra("danwei", s.getCompName());
                    in.putExtra("hfnum", s.getReplyCount() + "");
                    in.putExtra("url", s.getUserhead());
                    in.putExtra("content", s.getContent());
                    in.putExtra("title", s.getTitle());
                    in.putExtra("time", s.getCreateDate());
                    in.putExtra("collection", s.getIfCollect() + "");
                    startActivity(in);

                }
            });
            //收藏
            li_lt_collection.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (s.getIfCollect() == 1) {
                        getCancellCollectionData(holder.getLayoutPosition(), s.getQuestionId());
                    } else {
                        getCollectionData(holder.getLayoutPosition(), s.getQuestionId());
                    }
                }
            });

        }
    }

    public void doClick(View v) {
        Intent in;
        switch (v.getId()) {
            case R.id.rb_all://	回复状态0：未回复，1已回复
                statues = "";
                page = 1;
                getData();
                break;
            case R.id.rb_daihuida:
                statues = "0";
                page = 1;
                getData();
                break;
            case R.id.rb_answered:
                statues = "1";
                page = 1;
                getData();
                break;
            case R.id.tv_titlezixun://咨询
                type = 0;
                liZixun.setVisibility(View.VISIBLE);
                liLuntan.setVisibility(View.GONE);
                btFaqiluntan.setVisibility(View.GONE);
                tv_titlezixun.setBackgroundResource(R.drawable.ed_left_white);
                tv_titlezixun.setTextColor(getResources().getColor(R.color.MainColor));
                tv_titlelt.setBackgroundResource(R.drawable.ed_right_main);
                tv_titlelt.setTextColor(getResources().getColor(R.color.White));
                rbAll.performClick();
                break;
            case R.id.tv_titlelt://论坛
                type = 1;
                liZixun.setVisibility(View.GONE);
                liLuntan.setVisibility(View.VISIBLE);
                btFaqiluntan.setVisibility(View.VISIBLE);
                tv_titlelt.setBackgroundResource(R.drawable.ed_right_white);
                tv_titlelt.setTextColor(getResources().getColor(R.color.MainColor));
                tv_titlezixun.setBackgroundResource(R.drawable.ed_left_main);
                tv_titlezixun.setTextColor(getResources().getColor(R.color.White));
                page = 1;
                getLTListData();
                break;
            case R.id.bt_faqizixun://发起咨询
                in = new Intent(ZiXunActivity.this, FaQiZiXunActivity.class);
                in.putExtra("status", statues);
                startActivity(in);
                break;
            case R.id.bt_faqiluntan://发起论坛
                in = new Intent(ZiXunActivity.this, FaQiLunTanActivity.class);
                in.putExtra("status", statues);
                startActivity(in);
                break;
        }
    }
}
